package com.carleasoft.mps.adu.business.dept.controller;

import cn.hutool.core.util.StrUtil;
import com.carleasoft.mps.adu.business.dept.bean.entity.DeptEntity;
import com.carleasoft.mps.adu.business.dept.bean.po.DeptPagePo;
import com.carleasoft.mps.adu.business.dept.bean.po.DeptPo;
import com.carleasoft.mps.adu.business.dept.bean.vo.DeptTreeVo;
import com.carleasoft.mps.adu.business.dept.service.DeptService;
import com.carleasoft.mps.core.result.R;
import com.carleasoft.mps.core.user.Constants;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * <p>Description: 部门 controller</p>
 * @since 2020/4/28
 * @author ztm
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 添加部门或机构
     * @param deptPo
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody DeptPo deptPo) throws ParseException {
        IBeanUtil.paramVerify(deptPo, "deptName");
        if(StrUtil.isBlank(deptPo.getOrganizationCode())) deptPo.setOrganizationCode(ThreadUserContext.getCurrentCacheUser().getOrgCodeJoin());
        if(StrUtil.isBlank(deptPo.getParentDeptCode())) deptPo.setParentDeptCode(ThreadUserContext.getCurrentCacheUser().getOrgCodeJoin());
        return R.ok(deptService.add(deptPo));
    }

    /**
     * 删除部门或机构
     * @param deptPo
     * @return
     */
    @PostMapping("/delete")
    public R deleteDept(@RequestBody DeptPo deptPo) {
        IBeanUtil.paramVerify(deptPo, "deptCode");
        return  R.result(deptService.deleteDept(deptPo.getDeptCode()));
    }

    /**
     * 更新部门或机构信息
     * @param deptPo
     * @return
     */
    @PostMapping("/update")
    public R updateDept(@RequestBody DeptPo deptPo) {
        IBeanUtil.paramVerify(deptPo, "deptCode");
        return R.ok(deptService.updateDept(deptPo));
    }



    /**
     * 查询部门或机构信息
     * ok
     * @param deptPo
     * @return
     */
    @PostMapping("/deptDetail")
    public R findByDeptCode(@RequestBody DeptPo deptPo) {
        IBeanUtil.paramVerify(deptPo, "deptCode");
        return R.ok(deptService.findByDeptCode(deptPo));
    }

    /**
     * 查询部门页面信息
     * @param deptPagePo
     * @return
     */
    @PostMapping("/tree")
    public R treePageDept(@RequestBody DeptPagePo deptPagePo){
        if(StrUtil.isBlank(deptPagePo.getParentDeptCode())){
            if(StrUtil.isNotBlank(deptPagePo.getOrganizationCode())){
                deptPagePo.setParentDeptCode(deptPagePo.getOrganizationCode());
            }else {
                deptPagePo.setParentDeptCode(ThreadUserContext.getCurrentCacheUser().getOrgCodeJoin());
            }
        }
        return R.ok(deptService.treePageDept(deptPagePo));
    }

    @PostMapping("/getDeptListByParentCode")
    public R getByDeptList(@RequestBody DeptPagePo deptPagePo){
        if(StrUtil.isBlank(deptPagePo.getParentDeptCode())){
            if(StrUtil.isNotBlank(deptPagePo.getOrganizationCode())){
                deptPagePo.setParentDeptCode(deptPagePo.getOrganizationCode());
            }else {
                deptPagePo.setParentDeptCode(ThreadUserContext.getCurrentCacheUser().getOrgCodeJoin());
            }
        }
        return R.ok(deptService.getByDeptList(deptPagePo));
    }

    @GetMapping("getByDeptCode")
    public R<DeptEntity> getByDeptCode(@RequestParam("code") String code){
        return R.ok(deptService.getByDeptCode(code));
    }

    @PostMapping("/getAllDept")
    public R<List<DeptEntity>> getAllDept(){
        List<DeptEntity> deptAll = deptService.getDeptAll();
        return R.ok(deptAll);
    }
    /**
     * 不带入权限查部门树
     */
    @PostMapping("/cms/deptTree")
    public R deptTree(){
        return R.ok(deptService.deptTree());
    }
    /**
     * 不带入权限查部门树
     */
    @PostMapping("/cms/deptChildTree")
    public R deptChildTree(@RequestBody DeptTreeVo deptTreeVo){
        return R.ok(deptService.deptChildTree(deptTreeVo));
    }
}
